package dynamicCompile;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * @ClassName Demo02
 * @Description TODO
 * 通过反射运行指定的类
 * classload
 * @Author liang
 * @Date 2018/10/18 14:53
 * @Version 1.0
 **/
public class Demo02 {
    public static void main(String [] args) throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        String dir = "D:/a_code/dataSendClient/";
        URL[] urls = {new URL("file:/" + dir)};
        URLClassLoader loader = new URLClassLoader(urls);//加载的目录
        Class<?> clazz = loader.loadClass("Hi");//加载类
        //调用加载类的main方法
        Method method = clazz.getMethod("main", String[].class);
        /**
         * 由于可变参数是jdk5之后才有的。如果写成new String[]{"args1","args2"}就发生了参数个数不匹配问题
         * 因此，必须要加上object转型，避免这个问题
         */
        method.invoke(null,(Object) new String[]{});
    }


}
